//右上角按钮点击进入个人设置
import React, { useState, useEffect } from 'react';
import { Button, Drawer,Input,Modal ,AutoComplete,Collapse,
  Cascader,
  Checkbox,
  Col,
  Form,
  InputNumber,
  message,
  Row,
  Select,
option} from 'antd';
import { UserOutlined } from '@ant-design/icons';
import Link from 'next/link';
import Questionnaire from './questionnaire';
import DeleteUser from './deleteUser';
import LogOut from './logout';
import UpdateInfo from './update_info';
import { useRouter } from 'next/router';
import UpdatePassword from './update_password';
import axios from 'axios';//de了两个小时bug发现没引入？？？

const Set = () => {
  const router = useRouter();
 //打开抽屉
  const [open, setOpen] = useState(false);
  const [childrenDrawer, setChildrenDrawer] = useState(false);
  const showDrawer = () => {
    setOpen(true);
  };
  const onClose = () => {
    setOpen(false);
  };
  //抽屉子抽屉
  const showChildrenDrawer = () => {
    setChildrenDrawer(true);
  };
  const onChildrenDrawerClose = () => {
    setChildrenDrawer(false);
  };


  
  const [error, setError] = useState(null);
  const [userInfo, setUserInfo] = useState(null);

  
  const fetchUserInfo = async () => {
    try {
      const token = localStorage.getItem('token');
      const response = await axios.get('/api/user/info', {
        headers: {
          Authorization: `Bearer ${token}`
        }
      });
      console.log(response.data.data);
      message.success('获取信息成功');
      setUserInfo(response.data.data);
    } catch (error) {
      console.log('Error:', error);
      // 登录失败时的处理
      if (error.response) {
        // 服务器返回了错误响应
        const errorMessage = error.response.data.msg || '信息获取失败';
        message.error(errorMessage);
      } else if (error.request) {
        // 请求已发出，但没有收到响应
        message.error('请求失败，请检查网络连接');
      } else {
        // 发送请求时出了问题
        message.error('请求失败，请检查后端服务是否可用');
      }
    }
  };
  useEffect(() => {
      fetchUserInfo();
  }, []); // 只在组件挂载时执行一次

  
  return (
    <>
      <Button type="primary" onClick={showDrawer} icon={<UserOutlined />}>
        User
      </Button>
      <Drawer title="个人设置" onClose={onClose} open={open}>
    
          <p>Username:<Input value={userInfo?.username || ''}  /></p>
          <p>Email: <Input value={userInfo?.email || ''} /></p>
          <p>Sex: <Input value={userInfo?.sex || ''} /></p>
          <p>Risk Type: <Input value={userInfo?.risk_type || ''} /></p>
  

        <Button type="link" onClick={showChildrenDrawer}>
          重新进行风险评估
        </Button>
        <Drawer
          title="风险评估"
          width={500}
          closable={false}
          onClose={onChildrenDrawerClose}
          open={childrenDrawer}
        >
          <Questionnaire />
        </Drawer>
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />            
        <br />
        <br />
        <br />
        <UpdatePassword />/ <UpdateInfo />
        <br />
        <br />
        <LogOut />
        <br />
        <br />
        <DeleteUser />
      </Drawer>
    </>
  );
};
export default Set;